package server;

import java.util.NoSuchElementException;
import java.util.Vector;

/**
 * User Class:
 * Managed by UserManager1
 * Unique Username/Screenname
 * 
 * @author Fleischhacker
 *
 */
public class User {
	/**
	 * the UNIQUE username
	 */
	private String username;
	
	/**
	 * the UNIQUE screenname of the user
	 * state of LAB1 PART B: screenname = username
	 */
	private String screenname;
	
	/**
	 * Flag if the user is online (to recieve Notifications etc.)
	 */
	private boolean isOnline;
	
	/**
	 * notificationcontainer for the User (eg. overbid, won/lost Auction,...)
	 * FiFo
	 */
	private Vector<String> notifications;
	
	/**
	 * Public Constructor
	 * @param _username - User- and Screenname of the User
	 */
	public User(String _username){
		this.username = _username;
		this.screenname = _username;
		this.notifications = new Vector<String>();
		isOnline = false;
	}
	
	/**
	 * Adds a Notification for the User
	 * @param msg - Message for the User
	 */
	public void addNotification(String msg){
		notifications.add(msg);
	}
	
	/**
	 * Returns if there are any notifications for that user
	 * @return
	 */
	public boolean hasNotifications(){
		if(notifications.size() > 0)
			return true;
		return false;
	}
	
	/**
	 * Retrieves a Notification for the User and deletes it from the Container
	 * @return Message if there's one
	 * 			null otherwise
	 */
	public String getNotification(){
		synchronized(this){
			try{
				String msg = notifications.firstElement(); //NoSuchElementException if no messages => return null
				notifications.remove(0);
				return msg;
			}catch(NoSuchElementException nsee){
				//Container is Empty: no messages for that user
				return null;
			}catch(Exception e){
				//System.out.println("Error occured @User.getMessage():"+e.getMessage());
				return null;
			}
		}
	}
	
	/**
	 * Returns the Screenname
	 * @return
	 */
	public String getScreenname(){
		return new String(this.screenname);
	}
	
	/**
	 * Returns the Username
	 * @return
	 */
	public String getUsername(){
		return new String(this.username);
	}
	
	/**
	 * Returns Boolean if the User's online or not
	 * @return
	 */
	public boolean isOnline() {
		return isOnline;
	}

	/**
	 * Sets the User to Offline
	 * @param isOnline
	 */
	public void goOffline() {
		this.isOnline = false;
	}
	
	/**
	 * Sets the User to Online
	 * @param isOnline
	 */
	public void goOnline() {
		this.isOnline = true;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((screenname == null) ? 0 : screenname.hashCode());
		result = prime * result
				+ ((username == null) ? 0 : username.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		User other = (User) obj;
		if (screenname == null) {
			if (other.screenname != null)
				return false;
		} else if (!screenname.equals(other.screenname))
			return false;
		if (username == null) {
			if (other.username != null)
				return false;
		} else if (!username.equals(other.username))
			return false;
		return true;
	}
}
